package im.starchat.chat.view;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.squareup.picasso.Target;
import im.starchat.chat.R;
import im.starchat.chat.rest.model.TIMGiftModel;

public class GiftViewHolder implements Target{
    public ViewGroup root;
    public TextView nameTv;
    public TextView descTv;
    public ImageView headIcon;
//    public TextView giftCountTv;
    public ImageView giftIconIv;
//    public int giftCount;
    int[] giftIcons = new int[]{R.drawable.login_icon_weibo,
            R.drawable.login_icon_weixin, R.drawable.login_icon_weibo_p, R.drawable.login_icon_weixin_p};

    public void initViews(ViewGroup root) {
        this.root = root;
        root.setVisibility(View.INVISIBLE);
        nameTv = (TextView) root.findViewById(R.id.name);
        descTv = (TextView) root.findViewById(R.id.desc);
        headIcon = (ImageView) root.findViewById(R.id.avatar);
//        giftCountTv = (TextView) root.findViewById(R.id.gift_count);
        giftIconIv = (ImageView) root.findViewById(R.id.gift_icon);
        giftIconIv.setVisibility(View.INVISIBLE);
    }

    public void bindView(TIMGiftModel giftModel) {
        nameTv.setText(giftModel.getName());
        descTv.setText(giftModel.getDesc());
//        giftCountTv.setText("X" + giftCount);
//        this.giftCount = giftCount;
//        giftIconIv.setImageResource(giftIcons[giftIconId % giftIcons.length]);
        RoundDrawable roundDrawable =
                new RoundDrawable(BitmapFactory.decodeResource(root.getResources(), R.drawable.follows_head01));
        headIcon.setImageDrawable(roundDrawable);
        if (!TextUtils.isEmpty(giftModel.getHeadUrl())) {
            Picasso.with(root.getContext()).load(giftModel.getHeadUrl()).into(this);
        }
        if(!TextUtils.isEmpty(giftModel.getGiftUrl())){
            Picasso.with(root.getContext()).load(giftModel.getGiftUrl())
                    .into(giftIconIv);
        }
    }


    public void bindView(String headUrl, String name, String desc, String giftUrl) {
        nameTv.setText(name);
        descTv.setText(desc);
//        giftCountTv.setText("X" + giftCount);
//        this.giftCount = giftCount;
//        giftIconIv.setImageResource(giftIcons[giftIconId % giftIcons.length]);
        RoundDrawable roundDrawable =
                new RoundDrawable(BitmapFactory.decodeResource(root.getResources(), R.drawable.follows_head01));
        headIcon.setImageDrawable(roundDrawable);
        if (headUrl != null && headUrl.length() > 0) {
            Picasso.with(root.getContext()).load(headUrl).into(this);
        }
        if(!TextUtils.isEmpty(giftUrl)){
            Picasso.with(root.getContext()).load(giftUrl)
                    .into(giftIconIv);
        }
    }

    @Override
    public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
        if (bitmap == null) {
            return;
        }
        RoundDrawable roundDrawable =
                new RoundDrawable(bitmap);
        headIcon.setImageDrawable(roundDrawable);
    }

    @Override
    public void onBitmapFailed(Drawable errorDrawable) {}

    @Override
    public void onPrepareLoad(Drawable placeHolderDrawable) {}

//    public void setGiftCount(final int count) {
//        giftCount = count;
//        Animation hideAnim = AnimationUtils.loadAnimation(root.getContext(), R.anim.gift_count_hide_anim);
//        hideAnim.setAnimationListener(new SimpleAnimatonListener() {
//            @Override
//            public void onAnimationEnd(Animation animation) {
//                giftCountTv.setText("X" + count);
//                giftCountTv.postDelayed(new Runnable() {
//                    @Override
//                    public void run() {
//                        Animation show = AnimationUtils.loadAnimation(root.getContext(), R.anim.gift_count_show_anim);
//                        show.setAnimationListener(new SimpleAnimatonListener() {
//                            @Override
//                            public void onAnimationEnd(Animation animation) {
//                                giftCountTv.clearAnimation();
//                            }
//                        });
//                        giftCountTv.startAnimation(show);
//                    }
//                }, 0);
//            }
//        });
//        giftCountTv.startAnimation(hideAnim);
//    }

    public void show() {
        Animation animation = AnimationUtils.loadAnimation(root.getContext(), R.anim.gift_layout_show_animation);
        animation.setAnimationListener(new SimpleAnimatonListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                root.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                Animation headAnimation = AnimationUtils.loadAnimation(root.getContext(), R.anim.gift_icon_show_animation);
                headAnimation.setAnimationListener(new SimpleAnimatonListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {
                        giftIconIv.setVisibility(View.VISIBLE);
                    }
                });
                giftIconIv.startAnimation(headAnimation);
            }
        });
        root.startAnimation(animation);

    }

    public void dismiss() {
        Animation animation = AnimationUtils.loadAnimation(root.getContext(), R.anim.gift_layout_dismiss_animation);
        animation.setAnimationListener(new SimpleAnimatonListener() {
            @Override
            public void onAnimationEnd(Animation animation) {
                root.clearAnimation();
                root.setVisibility(View.INVISIBLE);
                giftIconIv.clearAnimation();
                giftIconIv.setVisibility(View.INVISIBLE);
            }
        });
        root.startAnimation(animation);
    }

    static class SimpleAnimatonListener implements Animation.AnimationListener {
        @Override
        public void onAnimationStart(Animation animation) {}
        @Override
        public void onAnimationEnd(Animation animation) {}
        @Override
        public void onAnimationRepeat(Animation animation) {}
    }
}
